//
//  ShellOrderViewModel.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/2.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellOrderViewModel.h"
@interface ShellOrderViewModel()

@property (nonatomic,assign) NSInteger pageIndex;

@end
@implementation ShellOrderViewModel
/**
 Convert 获取订单
 @param  isFirst 是否是第一次
 */
- (void)getOrderList:(BOOL)isFirst{

    if (isFirst) {
        self.pageIndex = 1;
        [self.dataArray removeAllObjects];
    }
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld",kQueryOrderList,(long)self.pageIndex];
    @weakify(self);
    [LZNetworkingManager  lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
       @strongify(self);
       dismiss(LZCurrentView);
       if([responseObject[kResultStatus] isEqualToString:kCode]){
           NSArray *dataArray = responseObject[kResultData];
           for (int i = 0; i < dataArray.count; i++) {
               ShellOrderModel *orderModel = [ShellOrderModel mj_objectWithKeyValues:dataArray[i]];
               [self.dataArray addObject:orderModel];
           }
               
           //分页查询没有更多数据, 或者是当前页面  ==  后台返回的总页数
           NSInteger totalCount = [responseObject[@"total"] integerValue];
           if(self.dataArray.count == totalCount){
               [self.reloadSubject sendNext:@(0)];
               return;
           }
           //看是否有数据
           if(self.dataArray.count == 0){
               [self.reloadSubject sendNext:@(-1)];
           } else {
               self.pageIndex++;
               [self.reloadSubject sendNext:@(1)];
           }
           } else {
               [self.reloadSubject sendNext:@(0)];
           }
   } failure:^(NSError * _Nullable error) {
       
   } notwork:^(NSError * _Nullable error) {
       
   } isLoading:NO isFailTip:YES];
}

/**
 Convert 获取订单详情
 */
- (void)getOrderDetail{
    
     NSString *url = [NSString stringWithFormat:@"%@/%ld",kReadOrderInfo,(long)self.orderModel.orderId];
     [LZNetworkingManager  lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
         if([responseObject[kResultStatus] isEqualToString:kCode]){


         }
     } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];

}


/**
 Convert 转出
 @param  orderCode 订单code
 @param  payPassword 交易密码
 */
- (void)tradeProcess:(NSString *)orderCode payPassword:(NSString *)payPassword{
    
    
    NSDictionary *params = @{@"orderCode":self.orderModel.code?:@"",
                             @"payPassword":payPassword?:@""};
    [LZNetworkingManager  lz_request:@"post" url:kProcess params:params success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            [LZToolView dismiss];
            showDelayedDismissTitle(LZLocalizedString(@"转出成功"), nil);
              dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                  [LZTool.currentViewController.navigationController popToRootViewControllerAnimated:YES];
              });
        }
    } failure:^(NSError * _Nullable error) {} notwork:^(NSError * _Nullable error) {} isLoading:YES isFailTip:YES];
    
    
    
}




@end
